package com.kvest.view_pager_test.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.kvest.view_pager_test.FragmentDialog.DialogUsingCreator;
import com.kvest.view_pager_test.R;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 17.03.13
 * Time: 21:49
 * To change this template use File | Settings | File Templates.
 */
public class SelfListFragment extends Fragment implements ListView.OnItemClickListener {
    private static final String ID_ARGUMENT = "id_argument";
    private static final String TAG_ARGUMENT = "tag_argument";

    public static SelfListFragment newInstamce(int id, String tag) {
        SelfListFragment result = new SelfListFragment();
        Bundle arguments = new Bundle();
        arguments.putInt(ID_ARGUMENT, id);
        arguments.putString(TAG_ARGUMENT, tag);
        result.setArguments(arguments);

        return result;
    }

    private int getIdArgument() {
        return getArguments().getInt(ID_ARGUMENT);
    }

    private String getTagArgument() {
        return getArguments().getString(TAG_ARGUMENT);
    }

    private ListView listView;
    private ArrayAdapter<String> adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.self_list_fragment, null);
        listView = (ListView)view.findViewById(R.id.self_list);
        listView.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, new ArrayList<String>());
        for (int i = 0; i < 10; ++i) {
            adapter.add("self list" + i);
        }
        adapter.add("id=" + getIdArgument());
        adapter.add("tag=" + getTagArgument());

        listView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        DialogUsingCreator dialog = new DialogUsingCreator(adapter.getItem(i));
        dialog.show(getFragmentManager(), null);
    }

    @Override
    public void onSaveInstanceState(Bundle savedObject) {
        Log.d("SAVE", "onSaveInstanceState");
        super.onSaveInstanceState(savedObject);
    }
}
